package com.tanfuqi.sicent.server.filter

import com.tanfuqi.sicent.protocol.message.Message
import org.apache.mina.core.session.IoSession
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory
import org.slf4j.LoggerFactory

class BeatAliveFactory(private val type:Int=0x8002) : KeepAliveMessageFactory {
    private val logger = LoggerFactory.getLogger(BeatAliveFactory::class.java)

    override fun isRequest(session: IoSession, message: Any): Boolean {
        return false
    }

    override fun isResponse(session: IoSession, message: Any): Boolean {
        if (message is Message) {
            if (message.type == (type xor 0x80000000L.toInt())&&session.getAttribute("shopId")!=null) {
                logger.info("响应心跳包信息: ${message.type}:${type xor 0x80000000L.toInt()}")

                return true
            }
        }
        return false
    }

    override fun getRequest(session: IoSession): Any {
        logger.info("请求预设信息: $type")
        /** 返回预设语句  */
        return Message(type)
    }

    override fun getResponse(session: IoSession, request: Any): Any? {
        return null
    }

}

